package com.fipl.taksar;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Variable2Activity extends Activity {

	private ArrayList<Integer> indexCheckArrayList = new ArrayList<Integer>();
	private ArrayList<Boolean>indexFlagArrayList = new ArrayList<Boolean>();
	private LinearLayout linearLayout;
	//	private int height;
	private ListView listView;
	private String [] variable={"Temperature_1", "Temperature_2", "Temperature_3" ,"Temperature_4", 
			"Humidity_1", "Humidity_2", "Humidity_3","Humidity_4","PSTemperature_1","PSTemperature_2",
			"Diesel_1","Diesel_2","Voltage_1A","Voltage_1B","Voltage_1C","Voltage_2A","Voltage_2B",
			"Voltage_2C","Voltage_3A","Voltage_3B","Voltage_3C","Voltage_4A","Voltage_4B","Voltage_4C",
			"Voltage_5A","Voltage_5B","Voltage_5C","Voltage_6A","Voltage_6B","Voltage_6C","Voltage_7A",
			"Voltage_7B","Voltage_7C","Voltage_8A","Voltage_8B","Voltage_8C","Current_1A","Current_1B",
			"Current_1C","Current_2A","Current_2B","Current_2C","Current_3A","Current_3B","Current_3C",
			"Current_4A","Current_4B","Current_4C","Current_5A","Current_5B","Current_5C","Current_6A",
			"Current_6B","Current_6C","DigitalInput_1","DigitalInput_2","DigitalInput_3","DigitalInput_4",
			"DigitalInput_5","DigitalInput_6","DigitalInput_7",	"DigitalInput_8","DigitalInput_9",
			"DigitalInput_10","DigitalInput_11","DigitalInput_12","DigitalInput_13","DigitalInput_14",
			"DigitalInput_15","DigitalInput_16","DigitalInput_17","DigitalInput_18","DigitalInput_19",
			"DigitalInput_20","DigitalInput_21","DigitalInput_22","DigitalInput_23","DigitalInput_24",
			"DigitalOutput_1","DigitalOutput_2","DigitalOutput_3","DigitalOutput_4","DigitalOutput_5",
			"DigitalOutput_6","DigitalOutput_7","DigitalOutput_8","DigitalOutput_9","DigitalOutput_10",
			"DigitalOutput_11","DigitalOutput_12","DigitalOutput_13","DigitalOutput_14","DigitalOutput_15",
			"DigitalOutput_16"
	};


	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.temp);

		CheckBox selectAll = (CheckBox)findViewById(R.id.tempselectChk);
		TextView selectallTxt = (TextView)findViewById(R.id.selectallTxt);

		LinearLayout insideLinearLayout = (LinearLayout)findViewById(R.id.insideRelative);
		insideLinearLayout.removeView(selectAll);
		insideLinearLayout.removeView(selectallTxt);

		//		Display display = getWindowManager().getDefaultDisplay();
		//		height = display.getHeight();
		getData();

	}

	// this method will sets the list view on the screen with the check box on right side of the variable name.

	public void getData(){

		setContentView(R.layout.temp);

		CheckBox selectAll = (CheckBox)findViewById(R.id.tempselectChk);
		TextView selectallTxt = (TextView)findViewById(R.id.selectallTxt);

		LinearLayout insideLinearLayout = (LinearLayout)findViewById(R.id.insideRelative);
		insideLinearLayout.removeView(selectAll);
		insideLinearLayout.removeView(selectallTxt);

		Button btnShowGraph = (Button)findViewById(R.id.tempshowGraph);
		btnShowGraph.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//				SetIndex(indexArrayList);
				if(indexCheckArrayList.size()==0){
					Toast.makeText(getApplicationContext(),"Please select atleast one variable", Toast.LENGTH_SHORT).show();
				}else{
					SetIndex();
				}	
			}
		});

		linearLayout = (LinearLayout)findViewById(R.id.middleVariable);
		listView = new ListView(this);
		listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

		MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this,variable);
		listView.setAdapter(myArrayAdapter);
		listView.setFocusable(true);
		listView.setBackgroundColor(Color.WHITE);
		listView.setVerticalScrollBarEnabled(true);
		listView.setCacheColorHint(Color.TRANSPARENT);
		linearLayout.addView(listView);

	}

	// this method will be called to set array list of the selected variables. and it bundle to new activity VariableGraph.
	public void SetIndex(){		
		Bundle bundle = new Bundle();
		bundle.putIntegerArrayList("indexCheck", indexCheckArrayList);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		intent.setClass(getApplicationContext(), VariableGraph.class);
		startActivity(intent);
	}

	/*
	 * to define custom list view
	 */
	public class MyArrayAdapter extends ArrayAdapter<String> {

		private final Activity context;
		private final String[] variable1;

		public MyArrayAdapter(Activity context, String[] variable) {
			super(context, R.layout.textlist, variable);
			this.context=context;
			this.variable1=variable;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();
			View rowView = inflater.inflate(R.layout.searchlist, null, true);
			TextView variableTextView = (TextView) rowView.findViewById(R.id.searchlistText1);
			CheckBox checkBox = (CheckBox)rowView.findViewById(R.id.wordChk);

			for(int i=0;i<indexCheckArrayList.size();i++){
				if(indexCheckArrayList.get(i) == position && indexFlagArrayList.get(i)){
					checkBox.setChecked(true);
				}
			}

			checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked){
						if(!(indexCheckArrayList.contains(position))){
							indexCheckArrayList.add(position);
							indexFlagArrayList.add(true);
						}
					}else{
						if(indexCheckArrayList.indexOf(position)!=-1){
							int indexPos = indexCheckArrayList.indexOf(position);
							indexCheckArrayList.remove(indexPos);
							indexFlagArrayList.remove(indexPos);
						}
					}
					notifyDataSetChanged();
				}
			});
			variableTextView.setText(variable1[position]);
			return rowView;
		}

	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	public void onStop(){
		super.onStop();
		Variable2Activity.this.finish();
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu (Menu menu){
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.option, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem){

		switch (menuItem.getItemId()) {

		case R.id.menuDb:
			((GlobalData)this.getApplicationContext()).setLoginFlag(false);
			((GlobalData)this.getApplicationContext()).setMenuNo(100);
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), ConfigurationActivity.class);
			startActivity(intent);
			break;

		case R.id.menuExit:
			Variable2Activity.this.finish();
			break;
		}
		return true;
	}

}
